/**
* @Title: indicatorsConfiguration_service.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author ex-xieqi001
* @date 2014年9月25日 上午10:39:31
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.service('IndicatorsConfigurationService',['$http','Modal','BaseURL','IndicatorsInfoService',
	                                              function($http,Modal,baseUrl,IndicatorsInfoService){
		var serv = this;
		serv.loading = false;
		serv.kpiId ='';
		serv.regionCode = '';	
		
		//获取所有统筹区指标
		this.getIndicatorTree = function(){
			 $http({
				 method : 'GET',
				 cache : false,
		    	 url: baseUrl + 'indicatorConfig/getIndicatorTree.do',
		    	 params :{temp:new Date()}
			}).success(function(response){
				serv.indicatorsTreeData = response.list;
				angular.forEach(serv.indicatorsTreeData,function(oneTreeData,i){
					oneTreeData.oldNodes = oneTreeData.nodes;
					angular.forEach(oneTreeData.nodes,function (data,j){
						if(data.kpiId == serv.kpiId && data.regionCode == serv.regionCode) {
							data.selected = true;
						}
					});
				});
			}).error(function(){
				Modal.alert("查询数据出错!!!");
			});
        };
        
    	//配置所有的信息
		this.addIndicatorAll = function(){
			serv.loading = true;
			$http({
				 method : 'GET',
				 cache : false,
		    	 url: baseUrl + 'indicatorConfig/addIndicatorAll.do',
		    	 params :{
		    		 		regionCode: serv.regionCode,
		    		 		temp:new Date()
		    	 		 }
			}).success(function(response){
				Modal.alert(response.msg);
				serv.getIndicatorTree();
				serv.loading = false;
			}).error(function(e){
				Modal.alert(e.msg);
				serv.loading = false;
			});
        };
        
        this.filter = function(text, cb) {
			angular.forEach(serv.indicatorsTreeData,function(oneTreeData,i){
				var dataNodes =[];
				if(text != null && text.trim().length >0){
					angular.forEach(oneTreeData.oldNodes,function (data,j){
						if(data.text.indexOf($.trim(text)) > -1){
							dataNodes.push(data);
						}
					});
				}
				else {
					dataNodes = oneTreeData.oldNodes;
				}
				if(dataNodes && dataNodes.length > 0) {
					oneTreeData.nodes = dataNodes;
				}
				else {
					oneTreeData.nodes = null;
				}
				
				
			});
		}
        
	}]).service('IndicatorsInfoService',['$http','Modal','BaseURL',
	                                              function($http,Modal,baseUrl){
		var serv = this;
		serv.regionCode ="";
		//获取单条指标详细信息
		this.queryIndicatorsInfo = function(kpiId,regionCode){
			$http({
				 method : 'GET',
				 cache : false,
		    	 url: baseUrl + 'indicatorConfig/getIndicatorsInfo.do',
		    	 params :{
		    		 temp:new Date(),
		    		 kpiId: kpiId,
		    		 regionCode: regionCode
		    		 }
			}).success(function(response){
				if(response.starts == 'Ok'){
					serv.regionCode = regionCode;
					serv.indicatorsInfo = response.info;
				}else{
					Modal.alert(response.msg);
				}
			}).error(function(e){
				Modal.alert(e.msg);
			});
        };
        
        //修改阈值
        this.updateIndicatorsInfoValue = function(data){
        	$http({
				 method : 'POST',
				 cache : false,
		    	 url: baseUrl + 'indicatorConfig/updateIndicatorsValue.do',
		    	 params :{
		    		 kpiId:data.kpiId,
		    		 id:data.id,
		    		 value:data.value,
		    		 type:data.type,
		    		 regionCode: serv.regionCode
		    		 }
			}).success(function(response){
				if(response.starts == 'Ok'){
					serv.starts = response.starts;
					serv.queryIndicatorsInfo(data.kpiId,serv.regionCode);
				}
				Modal.alert(response.msg);
			}).error(function(e){
				Modal.alert(e.msg);
			});
        }
        
      //修改预警规则阈值
        this.updateIndicatorsWarnValue = function(data){
        	$http({
				 method : 'POST',
				 cache : false,
		    	 url: baseUrl + 'indicatorConfig/updateIndicatorsWarnValue.do',
		    	 params :{
		    		 kpiId: data.kpiId,
		    		 id: data.id,
		    		 warnValue: data.warnValue,
		    		 regionCode: serv.regionCode
		    		 }
			}).success(function(response){
				if(response.starts == 'Ok'){
					serv.starts = response.starts;
					serv.queryIndicatorsInfo(data.kpiId,serv.regionCode);
				}
				Modal.alert(response.msg);
			}).error(function(e){
				Modal.alert(e.msg);
			});
        }
        
        //修改指标详细信息
        this.updateIndicatorsInfo = function(data){
        	$http({
				 method : 'POST',
				 cache : false,
		    	 url: baseUrl + 'indicatorConfig/updateIndicatorsInfo.do',
		    	 params :{
		    		 kpiId: serv.indicatorsInfo.kpiId,
		    		 kpiTypes: serv.indicatorsInfo.types,
		    		 orgTypes: serv.indicatorsInfo.orgs,
		    		 showType: serv.indicatorsInfo.showType,
		    		 visitType: serv.indicatorsInfo.visitType,
		    		 isValid: serv.indicatorsInfo.isValid,
		    		 regionCode: serv.regionCode
		    		 }
			}).success(function(response){
				if(response.starts == 'Ok'){
					
					serv.queryIndicatorsInfo(serv.indicatorsInfo.kpiId,serv.regionCode);
				}
				Modal.alert(response.msg);
			}).error(function(e){
				Modal.alert(e.msg);
			});
        }
        
	}]);
	
})();